package com.tec.security.utils.mail;

import com.tec.security.service.ViewsIPService;
import com.tec.security.service.ViewsService;
import com.tec.security.utils.captcha.CaptchaRedisService;
import com.tec.security.utils.response.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;

@Service
@Async("taskExecutor")
public class MailService {
    @Value("${spring.mail.username}")
    private String mail_s;
    private final CaptchaRedisService captchaRedisService;
    private final JavaMailSender mailSender;
    private final ViewsIPService viewsIPService;
    private final ViewsService viewsService;
    @Autowired
    public MailService(CaptchaRedisService captchaRedisService, JavaMailSender mailSender, ViewsIPService viewsIPService, ViewsService viewsService) {
        this.captchaRedisService = captchaRedisService;
        this.mailSender = mailSender;
        this.viewsIPService = viewsIPService;
        this.viewsService = viewsService;
    }

    public CompletableFuture<Result> commonEmail(Email email) throws MessagingException {
        boolean emailCheck = !Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email.getTos()[0]);
        if (emailCheck) {
            return CompletableFuture.completedFuture(Result.failure("邮箱格式错误", null));
        } else {
            //创建简单邮件消息
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            //谁发的
            message.setFrom(mail_s);
            //谁要接收
            message.setTo(email.getTos());
            //邮件标题
            message.setSubject("验证码");
            //邮件内容
            String code = randomAlphanumeric(6);
            message.setText("<h4>验证码:" + code + "</h4><br><h5>验证码10分钟内有效</h5>", true);
            try {
                mailSender.send(mimeMessage);
                captchaRedisService.setCaptcha(email.getTos()[0], code);
                return CompletableFuture.completedFuture(Result.success("发送成功", email));
            } catch (MailException e) {
                return CompletableFuture.completedFuture(Result.failure("发送失败", null));
            }
        }
    }


    public void commonScheduledEmail() throws MessagingException {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月dd日");
        Date _date = new Date(System.currentTimeMillis());
        String date=formatter.format(_date);
            //创建简单邮件消息
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            //谁发的
            message.setFrom(mail_s);
            //谁要接收
            message.setTo("XF-Studio@foxmail.com");
            //邮件标题
            message.setSubject(date+"网站数据");
            //邮件内容
            int todayUV=viewsService.todayHomeViews();
            int allUV=viewsService.allHomeViews();
            int todayPV=viewsIPService.todayIP_gain();
            int allPV=viewsIPService.allIP();
            message.setText("<h3>今日份网站报告UV:</h3><br>"+
                    "<h4>今日UV:" + todayUV + "</h4><br>" +
                    "<h4>全部UV:" + allUV + "</h4><br>" +
                    "<h4>今日PV:" + todayPV + "</h4><br>" +
                    "<h4>全部PV:" + allPV + "</h4><br>" +
                    "<h5>讯飞工作室官网后台管理系统</h5>"+
                    "<h5>--"+date+"</h5>",
                    true);
            try {
                mailSender.send(mimeMessage);
            } catch (MailException e) {
                System.out.println("发送失败");
            }
    }

    public void commonScheduledEmail_test() throws MessagingException {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月dd日");
        Date _date = new Date(System.currentTimeMillis());
        String date=formatter.format(_date);
        //创建简单邮件消息
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        //谁发的
        message.setFrom(mail_s);
        //谁要接收
        message.setTo("3186513948@qq.com");
        //邮件标题
        message.setSubject("今日网站数据"+date);
        //邮件内容
        int todayUV=viewsService.todayHomeViews();
        int allUV=viewsService.allHomeViews();
        int todayPV=viewsIPService.todayIP_gain();
        int allPV=viewsIPService.allIP();
        message.setText("<h3>今日份网站报告UV:</h3><br>"+
                        "<h4>今日UV:" + todayUV + "</h4><br>" +
                        "<h4>全部UV:" + allUV + "</h4><br>" +
                        "<h4>今日PV:" + todayPV + "</h4><br>" +
                        "<h4>全部PV:" + allPV + "</h4><br>" +
                        "<h5>讯飞工作室官网后台管理系统</h5>"+
                        "<h5>--"+date+"</h5>",
                true);
        try {
            mailSender.send(mimeMessage);
        } catch (MailException e) {
            System.out.println("发送失败");
        }
    }

    public CompletableFuture<Result> errorMail() throws MessagingException {
    return CompletableFuture.completedFuture(Result.failure("未找到与该邮箱绑定的账号", null));
    }
}
